import enterpriseServer from "@/static/js/enterpriseServer/index.js"
import choseRegion from '@/components/chose-region/chose-region.vue'
import wfAddress from "@/components/WF-address/WF-address.vue";
export default {
	components: {
		choseRegion,wfAddress
	},
	data() {
		return {
			//顶部tab栏切换
			current: 0,
			tabList: [{
				id: 0,
				name: '服务分类'
			},
			{
				id: 1,
				name: '服务商'
			}
			],

			//省市区
			choseRegion: [],
			choseRegion1: null,
			cityCode: '',

			//服务类目
			categoryShow: false,
			tabCur: 0,
			categoryList: [],
			cateFirstId: '',
			cateSecondId: '',
			cateSecondList: [],
			cateThirdId: '',
			cateName: '',
			cateSort: '',

			//排序
			sortValue: 0,
			sortData: ["综合排序", "官方服务商", "普通服务商", "成交量从高到低"],
			sortKey: 0,
			sortType: '',
			serviceType: '',
			//服务商品列表
			shopList: [],
			shopStatus: 'nomore',

			//服务商列表
			serviceList: [],
			serviceStatus: 'nomore',

			//分页参数
			servicePage: 1,
			servicePageSize: 10,
			shopPage: 1,
			shopPageSize: 10,
			isRefreshing: false,
			showCityList: false,
		};
	},
	onLoad(e) {
		if (e.c_id) {
			this.cateFirstId = e.c_id
			this.cateName = e.cateName
			this.tabCur = e.index
			this.cateSort = 'one'
		} else {
			this.current = e.index
		}
		this.init()
	},

	methods: {
		goBack() {
			uni.navigateBack({
				delta: 1
			})
		},

		//数据加载
		init() {
			if (this.current == 0) {
				this.getShopData()
			} else {
				this.servicePage = 1
				this.getServiceData()
			}
		},

		//前往搜索界面
		goSearch() {
			uni.navigateTo({
				url: '/enterpriseServer/search/index'
			})
		},

		//服务商和服务商品列表切换
		tabClick(item, index) {
			this.current = index
			this.choseRegion1 = null;
      this.cateName = "";
      this.cityCode = "";
			if (item.id == 0) {
				this.shopPage = 1
				this.getShopData()
			} else {
				this.servicePage = 1
				this.getServiceData()
			}
		},

		//省市切换
		regionName(res) {
			this.choseRegion = res
			if (res[1].id === 1) {
				this.cityCode = ''
				if (this.current === 0) {
					this.shopPage = 1
					this.getShopData()
				} else {
					this.servicePage = 1
					this.getServiceData()
				}
			} else {
				this.cityCode = res[1].id
				if (this.current === 0) {
					this.shopPage = 1
					this.getShopData()
				} else {
					this.servicePage = 1
					this.getServiceData()
				}
			}
		},
		regionValue(res) {
			console.log(res)
		},

		//综合排序
		changeSort(e) {
			console.log(e)
			this.sortKey = e.detail.value
			if (e.detail.value == 0) {
				this.sortType = 0
				this.serviceType = ''
			} else if (e.detail.value == 1) {
				this.serviceType = 1
				this.sortType = ''
			} else if (e.detail.value == 2) {
				this.serviceType = 5
				this.sortType = ''
			} else if (e.detail.value == 3) {
				this.sortType = 1
				this.serviceType = ''
			}
			if (this.current === 0) {
				this.shopPage = 1
				this.getShopData()
			} else {
				this.servicePage = 1
				this.getServiceData()
			}
		},

		cancelSort() {
      this.sortKey = 0;
      this.sortType = 0;
      this.serviceType = "";
      if (this.current == 0) {
        this.shopPage = 1;
        this.getShopData();
      } else {
        this.servicePage = 1;
        this.getServiceData();
      }
    },

		//服务分类选择全部
		choseAll() {
			this.cateName = ''
			this.cateFirstId = ''
			this.cateSecondId = ''
			this.cateThirdId = ''
			if (this.current === 0) {
				this.shopPage = 1
				this.getShopData()
			} else {
				this.servicePage = 1
				this.getServiceData()
			}
			this.categoryShow = false
		},

		//服务类目弹框
		showCategory() {
			this.getCategoryData()
			this.categoryShow = true
		},

		//获取服务类目
		async getCategoryData() {
			let res = await enterpriseServer.getServiceCate()
			if (res.code === 1) {
				this.categoryList = res.data
				this.cateFirstId = res.data[0].id
				this.getSecondCate()
			} else {
				this.$tools.msg(res.msg)
			}
		},

		//服务类目切换
		tabSelect(item, index) {
			this.tabCur = index;
			this.cateFirstId = item.id
			this.getSecondCate()
		},

		//获取二类三类服务类目
		async getSecondCate() {
			let obj = {
				c_id: this.cateFirstId
			}
			let res = await enterpriseServer.getServiceCateChild(obj)
			if (res.code === 1) {
				this.cateSecondList = res.data
			} else {
				this.$tools.msg(res.msg)
			}
		},

		//二级类目点击事件
		secondClick(val) {
			this.cateThirdId = ''
			this.cateName = val.name
			this.cateSort = 'two'
			this.cateSecondId = val.id
			this.cateFirstId = val.c_id
			this.categoryShow = false
			if (this.current === 0) {
				this.shopPage = 1
				this.getShopData()
			} else {
				this.servicePage = 1
				this.getServiceData()
			}
		},

		//三级类目点击事件
		thirdClick(val) {
			this.cateSort = 'three'
			this.cateName = val.name
			this.cateThirdId = val.id
			this.cateSecondId = val.s_id
			this.cateFirstId = val.c_id
			this.categoryShow = false
			if (this.current === 0) {
				this.shopPage = 1
				this.getShopData()
			} else {
				this.servicePage = 1
				this.getServiceData()
			}
		},

		//服务商品列表
		async getShopData() {
			this.shopStatus = 'loading'
			uni.showLoading({
				title: '加载中'
			})
			let obj = {
				cate: this.cateSort,
				city_code: this.cityCode,
				keyword: this.cateName,
				page: this.shopPage,
				pagesize: this.shopPageSize,
				sort: this.sortType,
				type: this.serviceType
			}
			let res = await enterpriseServer.getShopList(obj)
			if (res.code === 1) {
				let arr = []
				if (this.shopPage == 1) {
					this.shopList = []
				}
				let newList = res.data
				if (newList.length >= this.shopPageSize) {
					this.shopStatus = 'more'
				} else {
					this.shopStatus = 'nomore'
				}
				let arr1 = arr.concat(newList)
				arr1.forEach(val => {
					let obj = {
						id: val.id,
						name: val.name,
						pic: val.pic.split(','),
						price: val.price,
						serviceinfo: val.serviceinfo
					}
					this.shopList.push(obj)
				})
				this.isRefreshing = false
				uni.hideLoading()
			} else {
				this.$tools.msg(res.msg)
			}
		},

		//服务商列表
		async getServiceData() {
			this.serviceStatus = 'loading'
			uni.showLoading({
				title: '加载中'
			})
			let obj = {
				one_cate_id: this.cateFirstId,
				two_cate_id: this.cateSecondId,
				third_cate_id: this.cateThirdId,
				city_code: this.cityCode,
				sort: this.sortType,
				type: this.serviceType,
				page: this.servicePage,
				pageSize: this.servicePageSize
			}
			let res = await enterpriseServer.getServiceList(obj)
			if (res.code === 1) {
				let arr = []
				if (this.servicePage == 1) {
					this.serviceList = []
				}
				let newList = res.data.list
				if (newList.length >= this.servicePageSize) {
					this.serviceStatus = 'more'
				} else {
					this.serviceStatus = 'nomore'
				}
				let arr1 = arr.concat(newList)
				arr1.forEach(val => {
					let obj = {
						id: val.id,
						name: val.name,
						logo: val.logo,
						good_services: val.good_services,
						city: val.city,
						type: val.type
					}
					this.serviceList.push(obj)
				})
				this.isRefreshing = false
				uni.hideLoading()
			} else {
				this.$tools.msg(res.msg)
			}
		},

		//跳转服务商品详情
		goShopDetail(item) {
			uni.navigateTo({
				url: "/enterpriseServer/productInfo/index?id=" + item.id
			})
		},

		//跳转服务商详情
		goServiceInfo(item) {
			uni.navigateTo({
				url: '/enterpriseServer/merchantInfo/index?id=' + item.id
			})
		},

		//下拉刷新
		refresherrefresh() {
			this.isRefreshing = true;
			if (this.current == 0) {
				this.shopPage = 1
				this.getShopData()
			} else {
				this.servicePage = 1
				this.getServiceData()
			}
		},

		//上拉加载
		loadMore() {
			if (this.current == 0) {
				if (this.shopStatus == 'more') {
					this.shopPage += 1
					this.getShopData()
				}
			} else {
				if (this.serviceStatus == 'more') {
					this.servicePage += 1
					this.getServiceData()
				}
			}
		},

		showCityListFn() {
      this.showCityList = true;
    },

    closeCityList() {
      this.showCityList = false;
      this.choseRegion1 = null;
      this.cityCode = "";
      if (this.current == 0) {
        this.shopPage = 1;
        this.getShopData();
      } else {
        this.servicePage = 1;
        this.getServiceData();
      }
    },

    getCityInfo(data) {
      this.choseRegion1 = data;
      this.cityCode = data.id;
      this.showCityList = false;
      if (this.current == 0) {
        this.shopPage = 1;
        this.getShopData();
      } else {
        this.servicePage = 1;
        this.getServiceData();
      }
    },
	}
}